<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>平时成绩管理系统-成绩管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">学号</label>
                    <div class="layui-input-block">
                        <input type="text" name="studentId" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">姓名</label>
                    <div class="layui-input-block">
                        <input type="text" name="studentName" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">班级</label>
                    <div class="layui-input-block">
                        <select name="clazzId" id="clazzId"></select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">课程</label>
                    <div class="layui-input-block">
                        <select name="courseId" id="courseId"></select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-user-front-search"
                            data-type="reload">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layui-btn-danger" data-type="batchAdd">批量导入</button>
                <button class="layui-btn  layui-btn-normal" data-type="calculate">计算</button>
                <button class="layui-btn" data-type="reload">
                    <i class="layui-icon">&#xe601;</i>
                    <a href="http://192.168.1.108:9999/daily_score_management/excel/export" style="color: white"
                >EXCEL下载</a></button>
            </div>
            <table id="LAY-score-manage" lay-filter="LAY-score-manage"></table>

        </div>
    </div>
</div>

<script src="../../layuiadmin/layui/layui.js"></script>
<!--班级option选项模板-->
<script id="optionClazzId" type="text/html">
    <option value="">全部</option>
    {{#  layui.each(d.data, function(index, clazz){  }}
    <option value="{{clazz.clazzId}}">
        {{clazz.clazzYear}}级 {{clazz.clazzName}}
    </option>
    {{#  }); }}
    {{#  if(d.data.length === 0){ }}
    无数据
    {{#  } }}
</script>
<script type="text/html" id="course-type">
    {{# if (d.courseType === 0) { }}
    <span class="layui-badge layui-bg-blue">专业必修</span>
    {{# } else { }}
    <span class="layui-badge">专业选修</span>
    {{# }  }}
</script>
<!--课程option选项模板-->
<script id="optionCourse" type="text/html">
    <option value=""></option>
    {{#  layui.each(d.data, function(index, course){  }}
    <option value="{{course.courseId}}">
        {{course.courseId}}： {{course.courseName}}
    </option>
    {{#  }); }}
    {{#  if(d.data.length === 0){ }}
    无数据
    {{#  } }}
</script>
<script>

    function serializeArrayToJson(dataArr) {
        let dataMap = new Map();
        for (let item of dataArr) {
            dataMap.set(item.name, item.value);
        }

        if (dataMap.has("enable")) {
            dataMap.set("enable", 1);
        } else {
            dataMap.set("enable", 0);
        }
        let json = {};
        for (let [k, v] of dataMap) {
            json[k] = v;
        }
        return JSON.stringify(json);
    }

    layui.config({
        base: '../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'score', 'table', 'laytpl'], function () {
        let $ = layui.$
            , form = layui.form
            , laytpl = layui.laytpl
            , setter = layui.setter
            , table = layui.table;

        /*
        加载班级信息
        加载课程信息
        */
        $(function () {
            $.ajax({
                url: setter.baseUrl + '/clazz/ins',
                dataType: "json",
                type: "get",
                contentType: 'application/json',
                xhrFields: {
                    withCredentials: true
                },
                success: function (res) {
                    if (res.code === 200) {
                        let getTpl = optionClazzId.innerHTML
                            , view = document.getElementById('clazzId');
                        laytpl(getTpl).render(res, function (html) {
                            view.innerHTML = html;
                        });
                        form.render(); //更新全部
                    } else {
                        layer.msg("失败");
                    }

                },
                error: function (e) {
                    layer.msg("失败");
                }
            });
            $.ajax({
                url: setter.baseUrl + '/course/ins',
                dataType: "json",
                type: "get",
                contentType: 'application/json',
                xhrFields: {
                    withCredentials: true
                },
                success: function (res) {
                    if (res.code === 200) {
                        let getTpl = optionCourse.innerHTML
                            , view = document.getElementById('courseId');
                        laytpl(getTpl).render(res, function (html) {
                            view.innerHTML = html;
                        });
                        form.render(); //更新全部
                    } else {
                        layer.msg("失败");
                    }

                },
                error: function (e) {
                    layer.msg("失败");
                }
            });
        });
        //事件
        let active = {
            /*搜索*/
            reload: function () {
                let params = {
                    clazzId: $("select[name='clazzId']").val(),
                    courseId: $("select[name='courseId']").val(),
                    studentId: $("input[name='studentId']").val(),
                    studentName: $("input[name='studentName']").val(),
                };
                //执行重载
                table.reload('LAY-score-manage', {
                    page: {
                        curr: 1
                    }
                    , where: params
                });
            }
            , batchAdd: function () {
                layer.open({
                    type: 2
                    , title: 'Excel导入成绩计算'
                    , content: '../user/export_form.html'
                    , maxmin: true
                    , area: ['500px', '520px']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        let iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-user-export-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            table.reload('LAY-user-score-submit'); //数据刷新
                            layer.close(index); //关闭弹层
                        });
                        submit.trigger('click');
                    }
                });
            },
            calculate: function () {
                $.ajax({
                    url: setter.baseUrl + '/score/calculate',
                    dataType: "json",
                    type: "get",
                    contentType: 'application/json',
                    xhrFields: {
                        withCredentials: true
                    },
                    success: function (res) {
                        if (res.code === 200) {
                            layer.msg("计算已完成");
                            form.render(); //更新全部
                        } else {
                            layer.msg("失败，请检查授课教师以及计算规则是否存在！");
                        }
                    },
                    error: function (e) {
                        layer.msg("失败");
                    }
                });
            }
        };

        $('.layui-btn').on('click', function () {
            let type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>
